﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Gammar.Digger.BLL;
using Gammar.Digger.SystemFramework;
using Gammar.Digger.Entity;
public partial class status_runlog : System.Web.UI.Page
{
    public string systemStatus { get; set; }
    public int sysId { get; set; }
    public string currentStatusId { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
       
            string sysStatus;
            int sysid;
            bool isHave = SystemManagement.GetLastSystemStatus(out sysStatus, out sysid);
            systemStatus = sysStatus;
            sysId = sysid;
            string _statusid = Request.QueryString["statusid"];
        if (!IsPostBack)
        {
            if (isHave)
            {
                SortList<plugin_run_statusEntity> statusList = PluginManagement.GetRunPluginStatus(sysId);
                rptPlugins.DataSource = statusList;
                rptPlugins.DataBind();

                if (!string.IsNullOrEmpty(_statusid))
                {
                    ViewLogs(Convert.ToInt32(_statusid));
                }
            }
        }
    }

    protected string GetPluginName(string pluginid)
    {
        digger_pluginEntity entity= PluginManagement.GetPluginById(pluginid);

        return entity.name;
    }

    protected string GetRunStatus(object statusid)
    {
        if (statusid==null)
        {
            return "<font color='blue'>ready</font>";
        }
        else
        {
            return "<strong>"+ Enum.GetName(typeof(DataCollectService.Status), int.Parse(statusid.ToString()))+"</strong>";
        }
        
    }

    private void ViewLogs(int statusid)
    {
        txtPluginLogs.Text = string.Empty;
        SortList<plugin_run_logEntity> logList = PluginManagement.GetRunPluginLogs(statusid);
        foreach (plugin_run_logEntity log in logList)
        {
            string type = Enum.GetName(typeof(DataCollectService.event_type), log.event_type.Value);
            string reason = log.reason;
            string time = (new DateTime(log.create_time.Value)).ToString();
            string status_id = log.status_id.ToString();

            string message = type + "\t" + reason + "\t" + time + "\t" + status_id + "\n";
            txtPluginLogs.Text += message;
        }
    }

    protected void LinkButton_Command(Object sender, CommandEventArgs e)
    {
        ViewState["v_Status_id"] = e.CommandArgument;
        currentStatusId= "You chose: " + e.CommandName + " Item " + e.CommandArgument;
        Response.Redirect("runlog.aspx?statusid=" + e.CommandArgument.ToString());
        //ViewLogs(Convert.ToInt32(e.CommandArgument));
        
    }

    protected void rptPlugins_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        LinkButton lb=e.Item.FindControl("lbPluginLogs") as LinkButton;

        if (lb!=null)
        {
            lb.CommandName = "status_id";
            plugin_run_statusEntity row = e.Item.DataItem as plugin_run_statusEntity;
            lb.CommandArgument = row.status_id.ToString();
        }
    }
}
